package deamon

import (
	"time"

	"api/db"
	"api/loger"
)

var Activitys []db.Activity

// 初始化活动列表
func InitActivitys() {
	var err error
	Activitys = make([]db.Activity, 3)
	Activitys, err = db.SelectActivitys()
	if err != nil {
		loger.Loger.Println("error: init select activitys failed", err.Error())
	}

}

// 刷新活动列表
func RenewActivitys() {
	var err error
	Activitys, err = db.SelectActivitys()
	if err != nil {
		loger.Loger.Println("error: reselect activitys failed", err.Error())
	}
}
func SelectActivitys() []db.Activity {
	return Activitys
}

// 新添加一个活动
func InsertActivity(title, content, picture, link string, end_time time.Time) (err error) {
	newActivity := db.Activity{
		Title:   title,
		Content: content,
		Picture: picture,
		Link:    link,
		EndTime: end_time,
	}
	err = newActivity.Insert()
	if err != nil {
		loger.Loger.Println("error: insert new activity failed", err.Error())
		return err
	}
	RenewActivitys()
	return nil
}

// 主动过期一个活动
func FinishActivity(activityId int) (err error) {
	err = db.FinishActivity(activityId)
	if err != nil {
		loger.Loger.Println("error: finish activity failed", err.Error())
		return err
	}
	RenewActivitys()
	return err
}
